package cn.jboa.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import cn.jboa.dao.IGradeDAO;
import cn.jboa.entity.Grade;
@Repository
public class GradeDAOImpl implements IGradeDAO{
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Grade> findAll() {
		String sql="select * from grade";
		final List<Grade> list=new ArrayList<Grade>();
		jdbcTemplate.query(sql,new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Grade grade=new Grade();
				grade.setGradeId(rs.getInt("gradeid"));
				grade.setGradeName(rs.getString("gradename"));
				list.add(grade);
			}
		});
		return list;
	}

	@Override
	public int save(Grade grade) {
		String sql="insert into grade values(SQ_NUM.nextval,?)";
		int count= jdbcTemplate.update(sql, grade.getGradeName());
		return count;
	}

	@Override
	public int update(Grade grade) {
		// TODO Auto-generated method stub
		String sql="update grade set gradename=? where gradeid=?";
		int count= jdbcTemplate.update(sql, grade.getGradeName(), grade.getGradeId());
		return count;
	}

	@Override
	public List<Grade> findID(int id) {
		String sql="select * from grade where gradeid=?";
		final List<Grade> list=new ArrayList<Grade>();
		jdbcTemplate.query(sql, new RowCallbackHandler() {
			
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Grade grade=new Grade();
				grade.setGradeId(rs.getInt("gradeid"));
				grade.setGradeName(rs.getString("gradename"));
				list.add(grade);
				
			}
		},id);
		
		return list;
	}

	@Override
	public int delete(int id) {
		String sql="delete  from grade  where  gradeid=?";
		int update=jdbcTemplate.update(sql,id);
		return update;
	}

}












